<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Novedades extends CI_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('novedades_model');
		$this->load->library('session');
		$this->load->library('upload');
		$this->load->library('image_lib');
		$this->load->library('tank_auth');
		$this->load->helper('file');
		$this->load->helper('url');
		$this->load->helper('form');
		$this->load->helper('date');
		$this->load->helper('text');
		
		// prevent caching
		$this->output->set_header('Last-Modified:'.gmdate('D, d M Y H:i:s').'GMT');
		$this->output->set_header('Cache-Control: no-store, no-cache, must-revalidate');
		$this->output->set_header('Cache-Control: post-check=0, pre-check=0',false);
		$this->output->set_header('Pragma: no-cache');
		
		// if no session, redirect to login page		
		if(!$this->tank_auth->is_logged_in())
		{
			redirect('admin/login', 'refresh');
		}
	}
	
	public function index()
	{
		if($this->session->flashdata('message'))
		{
			$data['message'] = $this->session->flashdata('message');
		}
		$data['result'] = $this->novedades_model->get();		
		$data['title'] = 'Administrador - Starscamp';
		$data['session'] = array('username'=>ucfirst($this->tank_auth->get_username()));
		
		$this->load->view('admin/templates/header', $data);
		$this->load->view('admin/novedades', $data);
		$this->load->view('admin/templates/footer', $data);
	}
	
	public function registro($id = FALSE)
	{				
		// initialize upload
		$this->upload->initialize(array(
			'upload_path'=>'assets/photos/novedades/',
			'allowed_types'=>'gif|jpg|png',
			'max_size'=>4096
		));
		
		// This method will have the credentials validation
		$this->load->library('form_validation');		
		$this->form_validation->set_rules('name', 'Titulo', 'trim|required|xss_clean');
		$this->form_validation->set_rules('shortdesc', 'Descripción corta', 'trim|required|nl2br|xss_clean');
		$this->form_validation->set_rules('body', 'Cuerpo de texto', 'trim');
		$this->form_validation->set_rules('video', 'Video', 'trim|xss_clean|prep_url');
		$this->form_validation->set_rules('after', 'Cierre', 'trim');
		$this->form_validation->set_rules('deleted', 'Publicar', 'trim|xss_clean|is_natural|less_than[2]');
		
		if($this->form_validation->run())
		{
			$input = $this->input->post();
			
			// default values			
			if($this->input->post('deleted') === FALSE)
			{
				$input['deleted'] = 1;
			}
			$defaults = array('body'=>NULL, 'video'=>NULL, 'after'=>NULL);
			foreach($input as $key=>$value)
			{
				if($this->input->post($key) === '')
				{
					$input[$key] = $defaults[$key];
				}
			}
			$input['video'] = str_replace('vimeo.com/', 'player.vimeo.com/video/', $input['video']);
			$input['video'] = str_replace('youtube.com/watch?v=', 'youtube.com/embed/', $input['video']);
			
			if($_FILES && $_FILES['photo']['error'] === 0)
			{
				// A file was uploaded
				if($this->upload->do_upload('photo'))
				{
					$upload_data = $this->upload->data();
					$input['photo'] = 'assets/photos/novedades/'.$upload_data['file_name'];	
					
					// image
					$target['width'] = 200;
					$target['height'] = 113;
					$upload_aspect_ratio = $upload_data['image_width'] / $upload_data['image_height'];
					$target_aspect_ratio = $target['width'] / $target['height'];
					if($upload_aspect_ratio < $target_aspect_ratio)
					{
						$resize['width'] = $target['width'];
						$resize['height'] = $target['width'] * 10;
					}
					else
					{
						$resize['width'] = $target['height'] * 10;
						$resize['height'] = $target['height'];
					}
					
					// resize
					$this->image_lib->initialize(array(
						'image_library'=>'gd2',
						'source_image'=>'./assets/photos/novedades/'.$upload_data['file_name'],
						'maintain_ratio'=>TRUE,
						'width'=>$resize['width'],
						'height'=>$resize['height']
					));
					$this->image_lib->resize();
					// crop
					$this->image_lib->clear();
					$this->image_lib->initialize(array(
						'image_library'=>'gd2',
						'source_image'=>'./assets/photos/novedades/'.$upload_data['file_name'],
						'maintain_ratio'=>FALSE,
						'width'=>$target['width'],
						'height'=>$target['height'],
						'x_axis'=>0,
						'y_axis'=>0
					));
					$this->image_lib->crop();
					
					if($id !== FALSE)
					{
						$this->novedades_model->update($input, $id);
						$this->session->set_flashdata('message', 'Se ha modificado un registro');
					}
					else
					{
						$this->novedades_model->insert($input);
						$this->session->set_flashdata('message', 'Se ha insertado un registro');
					}
					redirect('admin/novedades', 'refresh');
				}
				else
				{
					$data['upload'] = $this->upload;
				}
			}
			else
			{
				if($id !== FALSE)
				{
					$this->novedades_model->update($input, $id);
					$this->session->set_flashdata('message', 'Se ha modificado un registro');
				}
				else
				{
					$this->novedades_model->insert($input);
					$this->session->set_flashdata('message', 'Se ha insertado un registro');
				}
				redirect('admin/novedades', 'refresh');
			}
		}
		$data['title'] = 'Administrador - Starscamp';
		$data['session'] = array('username'=>ucfirst($this->tank_auth->get_username()));
		if($id !== FALSE)
		{
			$data['row'] = $this->novedades_model->get_id($id);
			$data['row']->shortdesc = str_replace('<br />', '&#13;', $data['row']->shortdesc);
			$data['row']->video = str_replace('player.vimeo.com/video/', 'vimeo.com/', $data['row']->video);
			$data['row']->video = str_replace('youtube.com/embed/', 'youtube.com/watch?v=', $data['row']->video);
		}
		
		$this->load->view('admin/templates/header', $data);
		$this->load->view('admin/novedades-registro', $data);
		$this->load->view('admin/templates/footer', $data);
	}
	
	public function eliminar($id)
	{		
		$this->novedades_model->delete($id);
		
		$this->session->set_flashdata('message', 'Se ha eliminado un registro');
		redirect('admin/novedades', 'refresh');
	}
	
	public function rotator($id)
	{		
		if($this->session->flashdata('message'))
		{
			$data['message'] = $this->session->flashdata('message');
		}		
		$data['row'] = $this->novedades_model->get_id($id);
		$data['result'] = $this->novedades_model->get_slides($id);	
		$data['title'] = 'Administrador - Starscamp';
		$data['session'] = array('username'=>ucfirst($this->tank_auth->get_username()));
		
		$this->load->view('admin/templates/header', $data);
		$this->load->view('admin/novedades-rotator', $data);
		$this->load->view('admin/templates/footer', $data);
	}
	
	public function rotator_registro($id_novedad, $id = FALSE)
	{		
		// initialize upload
		$this->upload->initialize(array(
			'upload_path'=>'assets/photos/novedades_rotator/',
			'allowed_types'=>'gif|jpg|png',
			'max_size'=>4096
		));
		
		if($_FILES && $_FILES['url']['error'] === 0)
		{
			// A file was uploaded
			if($this->upload->do_upload('url'))
			{
				$upload_data = $this->upload->data();
				$input['id_novedad'] = $id_novedad;
				$input['url'] = 'assets/photos/novedades_rotator/'.$upload_data['file_name'];
					
					// image
					$target['width'] = 714;
					$target['height'] = 230;
					$upload_aspect_ratio = $upload_data['image_width'] / $upload_data['image_height'];
					$target_aspect_ratio = $target['width'] / $target['height'];
					if($upload_aspect_ratio < $target_aspect_ratio)
					{
						$resize['width'] = $target['width'];
						$resize['height'] = $target['width'] * 10;
					}
					else
					{
						$resize['width'] = $target['height'] * 10;
						$resize['height'] = $target['height'];
					}
					
					// resize
					$this->image_lib->initialize(array(
						'image_library'=>'gd2',
						'source_image'=>'./assets/photos/novedades_rotator/'.$upload_data['file_name'],
						'maintain_ratio'=>TRUE,
						'width'=>$resize['width'],
						'height'=>$resize['height']
					));
					$this->image_lib->resize();
					// crop
					$this->image_lib->clear();
					$this->image_lib->initialize(array(
						'image_library'=>'gd2',
						'source_image'=>'./assets/photos/novedades_rotator/'.$upload_data['file_name'],
						'maintain_ratio'=>FALSE,
						'width'=>$target['width'],
						'height'=>$target['height'],
						'x_axis'=>0,
						'y_axis'=>0
					));
					$this->image_lib->crop();
				
				if($id !== FALSE)
				{
					$this->novedades_model->update_slides($input, $id);
					$this->session->set_flashdata('message', 'Se ha modificado un registro');
				}
				else
				{
					$this->novedades_model->insert_slides($input);
					$this->session->set_flashdata('message', 'Se ha insertado un registro');
				}
				redirect('admin/novedades/rotator/'.$id_novedad, 'refresh');
			}
			else
			{
				$data['upload'] = $this->upload;
			}
		}
		
		$data['title'] = 'Administrador - Starscamp';
		$data['session'] = array('username'=>ucfirst($this->tank_auth->get_username()));		
		$data['row'] = $this->novedades_model->get_id($id_novedad);
		if($id !== FALSE)
		{
			$data['slide'] = $this->novedades_model->get_slides_id($id);
		}
		
		$this->load->view('admin/templates/header', $data);
		$this->load->view('admin/novedades-rotator-registro', $data);
		$this->load->view('admin/templates/footer', $data);
	}
	
	public function rotator_eliminar($id)
	{		
		$id_novedad = $this->novedades_model->get_slides_id($id)->id_novedad;
		$this->novedades_model->delete_slides($id);
		
		$this->session->set_flashdata('message', 'Se ha eliminado un registro');
		redirect('admin/novedades/rotator/'.$id_novedad, 'refresh');
	}
}

/* End of file novedades.php */
/* Location: ./application/controllers/admin/novedades.php */